import React, { useEffect, useState } from 'react';
import { Chrono } from 'react-chrono';
import { Empty } from 'antd';
import { useRequest, history } from 'umi';
import moment from 'moment';

export default (props: any) => {
  const [timeLineData, setTimeLineData] = useState<any>();
  const [rawData, setRawData] = useState<any>();

  //获取论文
  const findAllPaperByMutilPart = useRequest(
    (data = {}) => {
      return {
        url: 'http://10.1.40.84:9993/paper/findAllPaperByMutilPart',
        method: 'POST',
        headers: {
          Authorization: localStorage.getItem('token') || '',
        },
        data: {
          ...data,
          auth: 0, //覆盖data中的auth，个人看板默认展示本人的成果
        },
      };
    },
    {
      manual: true,
    },
  );
  //获取纵向项目
  const getVertProjList = useRequest(
    (data = {}) => {
      return {
        url: 'http://10.1.40.84:9998/project/getVertProjList',
        method: 'POST',
        headers: {
          Authorization: localStorage.getItem('token') || '',
        },
        data: { ...data, auth: 0 },
      };
    },
    {
      manual: true,
    },
  );
  //获取横向项目
  const getHoriProjList = useRequest(
    (data = {}) => {
      return {
        url: 'http://10.1.40.84:9998/project/getHoriProjList',
        method: 'POST',
        headers: {
          Authorization: localStorage.getItem('token') || '',
        },
        data: { ...data, auth: 0 },
      };
    },
    {
      manual: true,
    },
  );
  //获取国际项目
  const getInterProjList = useRequest(
    (data = null) => {
      return {
        url: 'http://10.1.40.84:9998/project/getInterProjList',
        method: 'POST',
        headers: {
          Authorization: localStorage.getItem('token') || '',
        },
        data: { ...data, auth: 0 },
      };
    },
    {
      manual: true,
    },
  );
  //获取申请专利
  const findAllPatentByMutilPartShenqing = useRequest(
    (data = {}) => {
      return {
        url: 'http://10.1.40.84:9993/patent/findAllPatentByMutilPart',
        method: 'POST',
        headers: {
          Authorization: localStorage.getItem('token') || '',
        },
        data: { ...data, auth: 0, patentType: '申请专利' },
      };
    },
    {
      manual: true,
    },
  );
  //获取授权专利
  const findAllPatentByMutilPartShouquan = useRequest(
    (data = {}) => {
      return {
        url: 'http://10.1.40.84:9993/patent/findAllPatentByMutilPart',
        method: 'POST',
        headers: {
          Authorization: localStorage.getItem('token') || '',
        },
        data: { ...data, auth: 0, patentType: '授权专利' },
      };
    },
    {
      manual: true,
    },
  );
  /**
   * 专著和奖励 参数问题！！！！！
   */
  //获取专著
  const findAllBookList = useRequest(
    (data = {}) => {
      return {
        url: 'http://10.1.40.84:9998/bookAndReward/getBookList',
        method: 'POST',
        headers: {
          Authorization: localStorage.getItem('token') || '',
        },
        data: { ...data, auth: 0 },
      };
    },
    {
      manual: true,
    },
  );
  //获取标准
  const findAllStandardByMutilPart = useRequest(
    (data = {}) => {
      return {
        url: 'http://10.1.40.84:9993/standard/findAllStandardByMutilPart',
        method: 'POST',
        headers: {
          Authorization: localStorage.getItem('token') || '',
        },
        data: { ...data, auth: 0 },
      };
    },
    {
      manual: true,
    },
  );
  //获取奖励
  const findAllRewardList = useRequest(
    (data = {}) => {
      return {
        url: 'http://10.1.40.84:9998/bookAndReward/getRewardList',
        method: 'POST',
        headers: {
          Authorization: localStorage.getItem('token') || '',
        },
        data: { ...data, queryType: 'personal' },
      };
    },
    {
      manual: true,
    },
  );
  //获取成果转换
  const findAllAchieveTransByMutilPart = useRequest(
    (data = {}) => {
      return {
        url: 'http://10.1.40.84:9993/achieveTrans/findAllAchieveTransByMutilPart',
        method: 'POST',
        headers: {
          Authorization: localStorage.getItem('token') || '',
        },
        data: { ...data, auth: 0 },
      };
    },
    {
      manual: true,
    },
  );
  //获取软件著作
  const findAllSoftWareCopyRightByMutilPart = useRequest(
    (data = {}) => {
      return {
        url: 'http://10.1.40.84:9993/software/findAllSoftWareCopyRightByMutilPart',
        method: 'POST',
        headers: {
          Authorization: localStorage.getItem('token') || '',
        },
        data: { ...data, auth: 0 },
      };
    },
    {
      manual: true,
    },
  );
  //获取仪器设备研制
  const findAllEquipmentDevelopByMutilPart = useRequest(
    (data = {}) => {
      return {
        url: 'http://10.1.40.84:9993/equipment/findAllEquipmentDevelopByMutilPart',
        method: 'POST',
        headers: {
          Authorization: localStorage.getItem('token') || '',
        },
        data: { ...data, auth: 0 },
      };
    },
    {
      manual: true,
    },
  );
  //获取其他标志性成果
  const findAllOtherAchieveByMutilPart = useRequest(
    (data = {}) => {
      return {
        url: 'http://10.1.40.84:9993/yearAndOther/findAllYearAndOtherAchieveByMutilPart',
        method: 'POST',
        headers: {
          Authorization: localStorage.getItem('token') || '',
        },
        data: { ...data, auth: 0, yearOrOther: '其他标志性成果' },
      };
    },
    {
      manual: true,
    },
  );
  //获取年度标志性成果
  const findAllYearAchieveByMutilPart = useRequest(
    (data = {}) => {
      return {
        url: 'http://10.1.40.84:9993/yearAndOther/findAllYearAndOtherAchieveByMutilPart',
        method: 'POST',
        headers: {
          Authorization: localStorage.getItem('token') || '',
        },
        data: { ...data, auth: 0, yearOrOther: '年度标志性成果' },
      };
    },
    {
      manual: true,
    },
  );

  //根据表名返回字段名
  const getDataField: any = () => {
    switch (props?.tableName) {
      case 'PAPER':
        return {
          title: 'year',
          cardTitle: 'paperTitle',
          cardSubtitle: 'authors',
        };
      case 'HORIZONTAL_PROJECT':
        return {
          title: 'startTime',
          cardTitle: 'projectName',
          cardSubtitle: 'director',
        };
      case 'VERTICAL_PROJECT':
        return {
          title: 'startTime',
          cardTitle: 'projectName',
          cardSubtitle: 'director',
        };
      case 'PATENT':
        return {
          title: 'year',
          cardTitle: 'patentName',
          cardSubtitle: 'authors',
        };
      case 'GRANT_PATENT':
        return {
          title: 'year',
          cardTitle: 'patentName',
          cardSubtitle: 'authors',
        };
      case 'INTER_CPT_PROJECT':
        return {
          title: 'startTime',
          cardTitle: 'projectName',
          cardSubtitle: 'director',
        };
      case 'ACHIEVE_TRANS':
        return {
          title: 'year',
          cardTitle: 'achieveName',
          cardSubtitle: 'authors',
        };
      case 'ACHIEVE_TRANS':
        return {
          title: 'year',
          cardTitle: 'bookName',
          cardSubtitle: 'authors',
        };
      case 'BOOK_PUBLISH':
        return {
          title: 'year',
          cardTitle: 'bookName',
          cardSubtitle: 'authors',
        };
      case 'STANDARD':
        return {
          title: 'year',
          cardTitle: 'standardName',
          cardSubtitle: 'authors',
        };
      case 'REWARD':
        return {
          title: 'year',
          cardTitle: 'rewardName',
          cardSubtitle: 'authors',
        };
      case 'ACHIEVE_TRANS':
        return {
          title: 'year',
          cardTitle: 'achieveName',
          cardSubtitle: 'authors',
        };
      case 'SOFTWARE_COPYRIGHT':
        return {
          title: 'year',
          cardTitle: 'achieveName',
          cardSubtitle: 'authors',
        };
      case 'EQUIPMENT_DEVELOP':
        return {
          title: 'year',
          cardTitle: 'equipName',
          cardSubtitle: 'authors',
        };
      case 'OTHER_ACHIEVE':
        return {
          title: 'year',
          cardTitle: 'achieveName',
          cardSubtitle: 'authors',
        };
      case 'YEAR_ACHIEVE':
        return {
          title: 'year',
          cardTitle: 'achieveName',
          cardSubtitle: 'authors',
        };
      default:
        return {};
    }
  };

  //调整数据格式适应timeline 根据tablename 组装数据
  const assembleDataTemp = (item: any) => {
    return {
      title: item[getDataField().title]
        ? moment(item[getDataField().title] + '').format('YYYY')
        : '无时间数据',
      cardTitle: item[getDataField().cardTitle] || '无成果名称',
      cardSubtitle: item[getDataField().cardSubtitle] || '无作者数据',
    };
  };

  //调整数据格式适应timeline
  const paramAdaptor = (dataSource: any) => {
    if (dataSource?.length && typeof dataSource != 'string') {
      let dataTemp: any = [];
      dataSource.map((item: any) => {
        dataTemp.push(assembleDataTemp(item));
      });
      dataTemp.sort((a: { title: string }, b: { title: string }) =>
        a.title.localeCompare(b.title),
      );
      return dataTemp;
    } else {
      return [];
    }
  };

  // 根据不同表名更改时间线数据
  const convertTimeLineData = (tableNameSelect: string) => {
    switch (tableNameSelect) {
      case 'PAPER':
        findAllPaperByMutilPart.run(props?.authorsInfo).then((data) => {
          setTimeLineData(paramAdaptor(data));
          setRawData(data);
        });
        break;
      case 'HORIZONTAL_PROJECT':
        getHoriProjList.run(props?.authorsInfo).then((data) => {
          setTimeLineData(paramAdaptor(data));
          setRawData(data);
        });
        break;
      case 'VERTICAL_PROJECT':
        getVertProjList.run(props?.authorsInfo).then((data) => {
          setTimeLineData(paramAdaptor(data));
          setRawData(data);
        });
        break;
      case 'PATENT':
        findAllPatentByMutilPartShenqing
          .run(props?.authorsInfo)
          .then((data) => {
            setTimeLineData(paramAdaptor(data));
            setRawData(data);
          });
        break;
      case 'GRANT_PATENT':
        findAllPatentByMutilPartShouquan
          .run(props?.authorsInfo)
          .then((data) => {
            setTimeLineData(paramAdaptor(data));
            setRawData(data);
          });
        break;
      case 'INTER_CPT_PROJECT':
        getInterProjList.run(props?.authorsInfo).then((data) => {
          setTimeLineData(paramAdaptor(data));
          setRawData(data);
        });
        break;
      case 'BOOK_PUBLISH':
        findAllBookList.run(props?.authorsInfo).then((data) => {
          setTimeLineData(paramAdaptor(data));
          setRawData(data);
        });
        break;
      case 'STANDARD':
        findAllStandardByMutilPart.run(props?.authorsInfo).then((data) => {
          setTimeLineData(paramAdaptor(data));
          setRawData(data);
        });
        break;
      case 'REWARD':
        findAllRewardList.run(props?.authorsInfo).then((data) => {
          setTimeLineData(paramAdaptor(data));
          setRawData(data);
        });
        break;
      case 'ACHIEVE_TRANS':
        findAllAchieveTransByMutilPart.run(props?.authorsInfo).then((data) => {
          setTimeLineData(paramAdaptor(data));
          setRawData(data);
        });
        break;
      case 'SOFTWARE_COPYRIGHT':
        findAllSoftWareCopyRightByMutilPart
          .run(props?.authorsInfo)
          .then((data) => {
            setTimeLineData(paramAdaptor(data));
            setRawData(data);
          });
        break;
      case 'EQUIPMENT_DEVELOP':
        findAllEquipmentDevelopByMutilPart
          .run(props?.authorsInfo)
          .then((data) => {
            setTimeLineData(paramAdaptor(data));
            setRawData(data);
          });
        break;
      case 'OTHER_ACHIEVE':
        findAllOtherAchieveByMutilPart.run(props?.authorsInfo).then((data) => {
          setTimeLineData(paramAdaptor(data));
          setRawData(data);
        });
        break;
      case 'YEAR_ACHIEVE':
        findAllYearAchieveByMutilPart.run(props?.authorsInfo).then((data) => {
          setTimeLineData(paramAdaptor(data));
          setRawData(data);
        });
        break;
      default:
        break;
    }
  };

  const onItemSelected = (item: any) => {
    console.log(item);
    console.log(rawData);
    if (item.title != '无成果名称') {
      localStorage.setItem('isEdit', '查看');
      var temp = rawData.filter((e: any) => {
        return e[getDataField().cardTitle] === item.cardTitle;
      });
      console.log(temp);

      var linkUrl: any;
      switch (props?.tableName) {
        case 'PAPER':
          linkUrl = '/LabAchievements/ScientificResearch/Paper/EditPaper/';
          break;
        case 'HORIZONTAL_PROJECT':
          linkUrl =
            '/LabAchievements/ScientificResearch/HorizontalProject/EditHorizontalProject/';
          break;
        case 'VERTICAL_PROJECT':
          linkUrl =
            '/LabAchievements/ScientificResearch/VerticalProject/EditVerticalProject/';
          break;
        case 'PATENT':
          linkUrl = '/LabAchievements/ScientificResearch/ApplyPatent/';
          break;
        case 'GRANT_PATENT':
          linkUrl = '/LabAchievements/ScientificResearch/GrantPatent/';
          break;
        case 'INTER_CPT_PROJECT':
          linkUrl =
            '/LabAchievements/ScientificResearch/InterCoopProject/EditInterCoopProject/';
          break;
        case 'BOOK_PUBLISH':
          linkUrl =
            '/LabAchievements/ScientificResearch/Monograph/EditMonograph/';
          break;
        case 'STANDARD':
          linkUrl =
            '/LabAchievements/ScientificResearch/Standard/EditStandard/';
          break;
        case 'REWARD':
          linkUrl = '/LabAchievements/ScientificResearch/Reward/EditReward/';
          break;
        case 'ACHIEVE_TRANS':
          linkUrl = '/LabAchievements/ScientificResearch/AchieveTransform/';
          break;
        case 'SOFTWARE_COPYRIGHT':
          linkUrl = '/LabAchievements/ScientificResearch/SoftwareCopyright/';
          break;
        case 'EQUIPMENT_DEVELOP':
          linkUrl = '/LabAchievements/ScientificResearch/ResearchEquipment/';
          break;
        case 'OTHER_ACHIEVE':
          linkUrl =
            '/LabAchievements/ScientificResearch/OtherAchievements/EditOtherAchievements/';
          break;
        case 'YEAR_ACHIEVE':
          linkUrl =
            '/LabAchievements/ScientificResearch/AnnualRepresentative/EditAnnualRepresentative/';
          break;
        default:
          break;
      }
      history.push(linkUrl + temp[0].id);
    }
  };
  useEffect(() => {
    convertTimeLineData(props?.tableName);
  }, [props?.tableName, props?.authorsInfo?.userId]);

  return (
    <div style={{ height: '400px' }}>
      {timeLineData && timeLineData.length ? (
        <Chrono
          items={timeLineData}
          cardHeight={100}
          cardWidth={600}
          hideControls={true}
          allowDynamicUpdate={true}
          onItemSelected={(item) => {
            onItemSelected(item);
          }}
        />
      ) : (
        <Empty image={Empty.PRESENTED_IMAGE_SIMPLE} />
      )}
    </div>
  );
};
